package sw6.NotificationMonkey;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;

import java.util.Timer;
import java.util.TimerTask;

public class NotificationService extends Service {

    private Timer mNotificationTimer;
    
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        setUpdateTimer(30 * 1000);
        
        return START_STICKY;
    }
    
    @Override
    public void onDestroy() {
        if (mNotificationTimer != null) {
            mNotificationTimer.cancel();
            mNotificationTimer = null;
        }
    }
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    private void setUpdateTimer(int millisecondsToUpdate) {
        mNotificationTimer = new Timer();
        mNotificationTimer.schedule(new NotificationTask(), millisecondsToUpdate);
    }

    class NotificationTask extends TimerTask {
        @Override
        public void run() {
            Intent intent = new Intent().setClass(NotificationService.this, NotificationActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            
            setUpdateTimer(30 * 1000);
        }
    }
}